//
//  PopoverControllerAnimationManager.swift
//  SinaWeibo
//
//  Created by paycloud110 on 16/6/3.
//  Copyright © 2016年 paycloud110. All rights reserved.
//

import UIKit

class PopoverControllerAnimationManager: UIPresentationController, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    
    private var presentFlag: Bool = false
    var presentedFrame: CGRect = CGRectZero
    
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let popoverVC = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        popoverVC.presentedFrame = presentedFrame
        return popoverVC
    }
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        presentFlag = true
        NSNotificationCenter.defaultCenter().postNotificationName(PopoverViewControllerShowClick, object: nil)
        return self
    }
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        presentFlag = false
        NSNotificationCenter.defaultCenter().postNotificationName(PopoverViewControllerDismissClick, object: nil)
        return self
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        //        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        //        JWNLog("\(toVC) + \(fromVC)")
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
        //        JWNLog("\(toView) + \(fromView)")
        
        if presentFlag {
            transitionContext.containerView()?.addSubview(toView!)
            toView?.transform = CGAffineTransformMakeScale(0.0, 1.0)
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                toView?.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    /** 必须告诉动画完成 */
                    transitionContext.completeTransition(true)
            }
        }else {
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.0000001)
                }) { (_) -> Void in
                    /** 必须告诉动画完成 */
                    transitionContext.completeTransition(true)
            }
        }
    }
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 1
    }
}
